package com.bawei.xiaowu.d1711.project.user.controller;


import com.alibaba.fastjson.JSON;
import com.bawei.xiaowu.d1711.project.common.MD5;
import com.bawei.xiaowu.d1711.project.common.ResponseResult;
import com.bawei.xiaowu.d1711.project.common.UserVo;
import com.bawei.xiaowu.d1711.project.user.dao.UserDao;
import com.bawei.xiaowu.d1711.project.user.entity.User;
import com.bawei.xiaowu.d1711.project.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2020-06-03
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 用户登录接口
     * @param user
     * @return
     */
    @PostMapping("login")
    public ResponseResult login(@RequestBody User user){
        //验证用户名和密码是否为空
        if(StringUtils.isBlank(user.getPassword()) || StringUtils.isBlank(user.getPassword())){
            return ResponseResult.error(10000,"用户名或密码不能为空");
        }
        //查询一条用户信息
        User userInfo = userDao.findByUsername(user.getUsername());
        //用户是否停用
        if(!userInfo.getEnabled()){
            return ResponseResult.error(10000,"用户名或密码不能为空");
        }
        //验证密码
        if(userInfo==null || !userInfo.getPassword().equals(user.getPassword())){
            return ResponseResult.error(10000,"用户名或密码不能为空");
        }
        //生成token信息 Jwt
        String token = MD5.encryptPassword(JSON.toJSONString(userInfo), System.currentTimeMillis() + "");
        userInfo.setToken(token);
        //把token信息存储到redis,同时存储用户信息
        redisTemplate.opsForValue().set(token,userInfo);
        redisTemplate.expire(token,60, TimeUnit.SECONDS);
        //设置密码为空
        userInfo.setPassword(null);
        userInfo.setUsername(null);
        return ResponseResult.success(userInfo);
    }

    /**
     * 根据token获取当前的用户信息
     * @param token
     * @return
     */
    @GetMapping("getLoginUserInfo")
    public UserVo getLoginUserInfo(@RequestParam("token") String token){
        User user = (User) redisTemplate.opsForValue().get(token);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);
        return userVo;
    }


    /**
     * 用户分页列表接口
     * @param
     * @return
     */
    @GetMapping("list")
    public ResponseResult list(User user,Integer current,Integer size){
        Page<User> pageInfo = userService.getPageInfo(user,current,size);
        return ResponseResult.success(null);
    }

    /**
     * 保存（新增或修改）
     * @param user
     * @return
     */
    @PostMapping("save")
    public ResponseResult save(@RequestBody  User user){
        //创建用户时，设置默认密码
        if(user.getId()==null){
            user.setPassword("123456");
        }
        //保存用户信息
        User save = userDao.save(user);
        return ResponseResult.success();
    }





}
